package com.health.healthproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.healthproject.common.Result;
import com.health.healthproject.dto.AnnouncementDTO;
import com.health.healthproject.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 创建公告
     */
    @PostMapping("/add")
    public Result<AnnouncementDTO> createAnnouncement(
            @Valid @RequestPart("announcementDTO") AnnouncementDTO announcementDTO,
            @RequestPart(value = "attachments", required = false) List<MultipartFile> attachments) throws IOException {
        return Result.success(announcementService.createAnnouncement(announcementDTO, attachments));
    }

    /**
     * 更新公告
     */
    @PutMapping("/{id}")
    public Result<AnnouncementDTO> updateAnnouncement(
            @PathVariable Long id,
            @Valid @RequestPart("announcement") AnnouncementDTO announcementDTO,
            @RequestPart(value = "attachments", required = false) List<MultipartFile> attachments) throws IOException {
        announcementDTO.setId(id);
        return Result.success(announcementService.updateAnnouncement(announcementDTO, attachments));
    }

    /**
     * 获取公告详情
     */
    @GetMapping("/{id}")
    public Result<AnnouncementDTO> getAnnouncement(@PathVariable Long id) {
        return Result.success(announcementService.getAnnouncement(id));
    }

    /**
     * 分页查询公告列表
     */
    @GetMapping("/list")
    public Result<Page<AnnouncementDTO>> listAnnouncements(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Long createBy,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "10") long size) {
        Page<AnnouncementDTO> page = new Page<>(current, size);
        return Result.success(announcementService.listAnnouncements(status,title,createBy,startDate,endDate, page));
    }

    /**
     * 发布公告
     */
    @PostMapping("/{id}/publish")
    public Result<Void> publishAnnouncement(@PathVariable Long id) {
        announcementService.publishAnnouncement(id);
        return Result.success();
    }

    /**
     * 撤回公告
     */
    @PostMapping("/{id}/recall")
    public Result<Void> recallAnnouncement(@PathVariable Long id) {
        announcementService.recallAnnouncement(id);
        return Result.success();
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/deleted/{id}")
    public Result<Void> deleteAnnouncement(@PathVariable Long id) {
        announcementService.deleteAnnouncement(id);
        return Result.success();
    }

    @GetMapping("/latest")
    public Result<List<AnnouncementDTO>> latest(){
        return announcementService.latest();
    }
}